package com.hotel.service;

import com.hotel.mapper.AdminMapper;
import com.hotel.mapper.StaffMapper;
import com.hotel.pojo.Admin;
import com.hotel.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private StaffMapper staffMapper;

    // 注册酒店工作人员账号
    public boolean registerStaff(Staff staff){
        Staff staffByUserName = staffMapper.findStaffByUserName(staff.getUsername());
        Staff staffByName = staffMapper.findStaffByName(staff.getName());
        if(staffByUserName != null || staffByName != null) {
            return false;
        } else {
            staffMapper.addStaff(staff);
            return true;
        }
    }

    // 更新管理员信息
    public void updateAdmin(Admin admin){
        adminMapper.updateAdmin(admin);
    }

    // 查看管理员是否存在
    public boolean findAdminByName(String name) {
        Admin admins = adminMapper.findAdminByName(name);
        return admins != null;
    }

    // 检查管理员账号密码
    public boolean checkAdmin(Admin admin) {
        List<Admin> adminList = adminMapper.findAllAdmin();
        List<Admin> collect = adminList.stream().filter(admin1 -> admin.getUsername().equals(admin1.getUsername())
                && admin.getPassword().equals(admin1.getPassword())).collect(Collectors.toList());
        return !collect.isEmpty();
    }

    // 管理员登录
    public void loginAdmin(String name) {
        adminMapper.loginAdmin(name);
    }

    // 管理员注销
    public void exitAdmin(String name) {
        adminMapper.exitAdmin(name);
    }

    // 查看管理员是否登录
    public boolean isLogin(String name) {
        return adminMapper.isLogin(name) == 1;
    }

}
